<?php

declare(strict_types=1);

use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\AsyncQueue\JobInterface;
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Hyperf\Utils\ApplicationContext;

if (!function_exists('di')) {
    /**
     * Finds an entry of the container by its identifier and returns it.
     * @param null|mixed $id
     * @return mixed|\Psr\Container\ContainerInterface
     */
    function di($id = null)
    {
        $container = ApplicationContext::getContainer();
        if ($id) {
            return $container->get($id);
        }
        return $container;
    }
}

if (!function_exists('logger')) {
    /**
     * 文件日志
     */
    function logger($name = 'hyperf', $group = 'default')
    {
        return di()->get(\Hyperf\Logger\LoggerFactory::class)->get($name, $group);
    }
}

if (!function_exists('format_throwable')) {
    /**
     * Format a throwable to string.
     */
    function format_throwable(Throwable $throwable): string
    {
        return di()->get(FormatterInterface::class)->format($throwable);
    }
}

if (!function_exists('queue_push')) {
    /**
     * Push a job to async queue.
     */
    function queue_push(JobInterface $job, int $delay = 0, string $key = 'default'): bool
    {
        $driver = di()->get(DriverFactory::class)->get($key);
        return $driver->push($job, $delay);
    }
}

if (!function_exists('jsonEncode')) {
    /**
     * json unescaped unicode
     * json  不中文编码
     */
    function jsonEncode($data)
    {
        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('jsonDecode')) {
    /**
     * json return array
     */
    function jsonDecode($data)
    {
        return json_decode($data, true);
    }
}

if (!function_exists('pregReplace')) {
    /**
     * 正则减除 多余符号
     */
    function pregReplace($data)
    {
        return preg_replace("/\/|\~|\!|\@|\￥|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|/", "", $data);
    }
}

if (!function_exists('isTbUrlOrOther')) {
    /*
     * 判断是否是淘口令或 其他链接
     */
    function isTbUrlOrOther($data)
    {
        $tkl = "/[\p{Sc}|\(|€|₤|₳|¢|₴|\/]\w{8,12}[\p{Sc}|\)|€|₤|₳|¢|₴|\/]/";
        if (preg_match($tkl, $data, $res)) {
            return pregReplace($res[0]);
        } elseif (strpos($data, 'jd.com') !== false) {
            return $data;
        } elseif (strpos($data, 'pinduoduo.com') !== false) {
            return $data;
        } elseif (strpos($data, 'yangkeduo.com') !== false) {
            return $data;
        } elseif (strpos($data, 'suning.com') !== false) {
            return $data;
        } elseif (strpos($data, 'vip.com') !== false) {
            return $data;
        } elseif (strpos($data, 'tb.cn') !== false) {
            return $data;
        } elseif (strpos($data, 'taobao.com') !== false) {
            return $data;
        }
    }
}

if (!function_exists('cut')) {
    /**
     * 截取字符串 指定字段
     */
    function cut($begin, $end, $str)
    {
        $b = mb_strpos($str, $begin) + mb_strlen($begin);
        $e = mb_strpos($str, $end) - $b;
        return mb_substr($str, $b, $e);
    }
}

if (!function_exists('combineURL')) {
    /**
     * 建数组转为url链接参数
     * @param $baseURL
     * @param $keysArr
     * @return string
     */
    function combineURL($baseURL, $keysArr)
    {
        $combined = $baseURL . "?";
        $valueArr = array();

        foreach ($keysArr as $key => $val) {
            $valueArr[] = "$key=$val";
        }
        $keyStr = implode("&", $valueArr);
        $combined .= ($keyStr);
        return $combined;
    }
}

if (!function_exists('getImageTemplate')) {
    /**
     * 转换为可用参数
     * @param $wxId
     * @param $content
     * @param $mediaInfos
     * @param $description
     * @param string $title
     * @return string
     */
    function getImageTemplate($wxId, $content, $mediaInfos, $description, $title = '')
    {
        if ($mediaInfos != null && count($mediaInfos) > 0) {
            $media = '';
            foreach ($mediaInfos as $item) {
                $media .= '<media><id>13337689008670904387</id><type>' . $item['type'] . '</type><title></title><description></description><private>0</private><url type=\"1\">' . $item['url'] . '</url><thumb type=\"1\">' . $item['thumbUrl'] . '</thumb><size height=\"' . $item['height'] . '\" width=\"' . $item['width'] . '\" totalSize=\"' . $item['totalLen'] . '\"></size></media>';
            }
        }
        $template = '<TimelineObject><id>1</id><username>' . $wxId . '</username><createTime>' . time() . '</createTime><contentDescShowType>0</contentDescShowType><contentDescScene>0</contentDescScene><private>0</private><contentDesc>' . $content . '</contentDesc><contentattr>0</contentattr><sourceUserName></sourceUserName><sourceNickName></sourceNickName><statisticsData></statisticsData><weappInfo><appUserName></appUserName><pagePath></pagePath></weappInfo><canvasInfoXml></canvasInfoXml><ContentObject><contentStyle>1</contentStyle><contentSubStyle>0</contentSubStyle><title>' . $title . '</title><description>' . $description . '</description><contentUrl></contentUrl><mediaList>' . $media . '</mediaList></ContentObject><actionInfo><appMsg><mediaTagName></mediaTagName><messageExt></messageExt><messageAction></messageAction></appMsg></actionInfo><appInfo><id></id></appInfo><location poiClassifyId=\"\" poiName=\"\" poiAddress=\"\" poiClassifyType=\"0\" city=\"\"></location><publicUserName></publicUserName><streamvideo><streamvideourl></streamvideourl><streamvideothumburl></streamvideothumburl><streamvideoweburl></streamvideoweburl></streamvideo></TimelineObject> ';
        return $template;
    }
}

if (!function_exists('strToUtf8')) {
    function strToUtf8($str)
    {
        $encode = mb_detect_encoding($str, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
        if ($encode == 'UTF-8') {
            return $str;
        } else {
            return mb_convert_encoding($str, 'UTF-8', $encode);
        }
    }
}

if (!function_exists('strToByte')) {
    function strToByte($string)
    {
        $bytes = array();
        for ($i = 0; $i < strlen($string); $i++) {
            $bytes[] = ord($string[$i]);
        }
        return $bytes;
    }
}

if (!function_exists('byteToStr')) {
    function byteToStr($bytes)
    {
        $str = '';
        foreach ($bytes as $ch) {
            $str .= chr($ch);
        }
        return $str;
    }
}

if (!function_exists('getRand')) {
    /**
     * 生成概率随机数
     * @param $proArr
     * @return int|string
     */
    function getRand($proArr)
    {
        $result = '';
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }
}
